<?
//这个是查看当前客户列表中，看看那些是已经超时的，超时的就自动发送邮件。
include_once("../../config.php");
$_SESSION['database'] = $_unit[$_REQUEST['athloncode']]['database'];


//只在登录的时候需要再引入一次。登录之后就会产生session，会在config中引入
include_once (ROOT_PATH_."/connection.php");


//获取超时需要联系的客户；提取出对应的类型的email
print_r2($_SESSION);


//查找用户，离checkpoint 最远的一个
$_member_checkpoint=
select_lines_all_items($table_prefix.'member',
                    "checkpoint asc",
                    "checkpoint < '".date("Y-m-d")."'",
                     "1");

//print_r2($_member_checkpoint_list);
//echo $_member_checkpoint_list[0][nid];

//找到了用户，现在要开始随机调用邮件发送



$now_agent=select_one_line_all_items($table_Prefix."employee","nid = '".$_member_checkpoint['0']['agentnid']."'");



//print_r2($now_agent);
//print_r2($now_agent);

//引入页面需要设置以下的值才能成功发送
$_mail_setting['smtp_account']  = $now_agent['smtp_account'];
$_mail_setting['smtp_password'] = $now_agent['smtp_password'];
$_mail_setting['smtp_host']     = $now_agent['smtp_host'];
$_mail_setting['smtp_port']     = $now_agent['smtp_port'];
$_mail_setting['title']         = $_REQUEST['mail_theme'];
$_mail_setting['content']          = $_REQUEST['ai_content'];
$_mail_setting['receiver']         = $_REQUEST['receiver'];
$_mail_setting['mail_email']       = $_REQUEST['mail_email'];

//没有发件人名字，就用邮箱做发件人。
if(!$_REQUEST['mail_sender'])
{
    $_REQUEST['mail_sender']=$_mail_setting['smtp_account'];
}

$_mail_setting['mail_sender']       = $_REQUEST['mail_sender'];


print_r2($_mail_setting);

exit;


//电子邮件发送程序
//引入发送email必须的文件
require (ROOT_PATH_."/application/PHPMailer/sendmail.php");



//todo 需要更新一下下次联系的时间。
$_REQUEST['ai_contact_way']   = "电子邮件";
$member_log['ai_authur']      = $_SESSION['username'];
$member_log['ai_ip']          = $_SERVER["REMOTE_ADDR"];
$_REQUEST['ai_name']          = $_REQUEST['mail_theme'];

$member_log['ai_content']     = $_REQUEST['member_log_content'];
$member_log['ai_excutor']     = $_SESSION['nid'];
$member_log['ai_membernid']   = $_REQUEST['nid']; //这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权
$member_log['ai_unitnid']     = $_REQUEST['unitnid'];//这个request 极度容易会在页面中被修改成其他id；目前处理方法就是顶部鉴权



$sql=insert_date($table_prefix."member_contact_log",$_REQUEST);
$query = mysql_query($sql) or die ("error:".mysql_error().$sql);



//当前联系人信息
$now_member=select_one_line_all_items($table_Prefix."member","nid = '".$_REQUEST ['ai_membernid']."'");
//当前业务员信息



    if(date('Y-m-d')>$now_member['checkpoint'])
        {
            //echo "已经超时";
            //echo "<br/>";
            //echo "<br/>";

            for($i=1;date('Y-m-d')>=$new_checkpoint;$i++)
            {

                if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i day"  ,strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 3 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i week" ,strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 7 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i month",strtotime($now_member['checkpoint'])));}
                if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 30 ) ){$new_checkpoint=date("Y-m-d",strtotime("+$i year" ,strtotime($now_member['checkpoint'])));}

                echo "循环内的新时间";
                echo $new_checkpoint;
                echo "<br/>";
                echo "<br/>";
            }

        }
    else
        {
            //echo "未超时,必须要在checkpoint以后的几天联系才能更新时间";
            //echo count_days(date('Y-m-d'),$now_member['checkpoint']);
            //echo "<br/>";

            if(($now_member['period']=='每日') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) == 0 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 day"  ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每周') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 6 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 week" ,strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每月') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 23 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 month",strtotime($now_member['checkpoint'])));}
            if(($now_member['period']=='每年') && ( count_days(date('Y-m-d'),$now_member['checkpoint']) < 335 ) ){$new_checkpoint=date("Y-m-d",strtotime("+1 year" ,strtotime($now_member['checkpoint'])));}

        }


if($new_checkpoint)
{
    $newcheckpoint['ai_checkpoint']=$new_checkpoint;
    $sql=update_date($table_prefix."member",$newcheckpoint,"nid",$_REQUEST ['ai_membernid']);
    $query = mysql_query($sql) or die ("error:".mysql_error());
}





?>



